package com.xiaoda.controller;

import com.xiaoda.entity.JsonResult;
import com.xiaoda.entity.Like;
import com.xiaoda.entity.User;
import com.xiaoda.service.LikeService;
import com.xiaoda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 喜欢管理
 */

@Controller
@CrossOrigin
@RequestMapping("/like")
@ResponseBody
public class LikeController {

    @Autowired
    LikeService likeService;

    @Autowired
    UserService userService;

    /**
     * 查询推荐关注列表
     *
     * @param uid 用户 id
     * @return 推荐关注列表
     */
    @GetMapping("/attention")
    public JsonResult attention(@RequestParam("uid") String uid) {
        JsonResult json = new JsonResult();
        List<User> list = userService.recommendedAttention(uid);
        List<Map<String, Object>> mlist = new ArrayList<>();
        if (list == null || list.isEmpty()) {
            json.setCode(404);
            json.setMsg("暂无推荐关注的用户！！！");
        } else {
            json.setCode(200);
            json.setMsg("推荐关注的用户列表数据查询成功！！！");
            for (User user : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("account", user.getAccount());
                map.put("avatar", user.getAvatar());
                map.put("nickname", user.getNickname());
                map.put("collect", likeService.findByUid(uid, user.getAccount()) != null);
                mlist.add(map);
            }
        }
        json.setData(mlist);
        return json;
    }

    /**
     * 关注 || 取关 用户
     *
     * @param fromUid 用户 id
     * @param toUid   关注用户 id
     * @param collect 关注状态
     * @return 关注结果
     */
    @PostMapping("/attention")
    public JsonResult attention(@RequestParam("fromUid") String fromUid,
                                @RequestParam("toUid") String toUid,
                                @RequestParam("collect") boolean collect) {
        JsonResult json = new JsonResult();
        if (collect) {
            User u1 = new User();
            u1.setAccount(fromUid);
            User u2 = new User();
            u2.setAccount(toUid);
            int row = likeService.addLike(new Like(0, u1, u2, null));
            if (row > 0) {
                json.setCode(200);
                json.setMsg("关注用户成功！！！");
            } else {
                json.setCode(404);
                json.setMsg("关注用户失败！！！");
            }
        } else {
            int row = likeService.delLikeByUid(fromUid, toUid);
            if (row > 0) {
                json.setCode(200);
                json.setMsg("取消关注用户成功！！！");
            } else {
                json.setCode(404);
                json.setMsg("取消关注用户失败！！！");
            }
        }
        json.setData(null);
        return json;
    }

    /**
     * 赞同 || 取消赞同 帖子
     *
     * @param isLiked 赞同状态
     * @param fromUid 用户 id
     * @param toAid   帖子 id
     * @return 赞同结果
     */
    @PostMapping("/article")
    public JsonResult article(@RequestParam("isLiked") boolean isLiked,
                              @RequestParam("fromUid") String fromUid,
                              @RequestParam("toAid") int toAid) {
        JsonResult json = new JsonResult();
        if (isLiked) {
            int row = likeService.addLikeArticle(fromUid, toAid);
            if (row > 0) {
                json.setCode(200);
                json.setMsg("赞同帖子成功！！！");
            } else {
                json.setCode(404);
                json.setMsg("赞同帖子失败！！！");
            }
        } else {
            int row = likeService.delLikeArticle(fromUid, toAid);
            if (row > 0) {
                json.setCode(200);
                json.setMsg("取消赞同帖子成功！！！");
            } else {
                json.setCode(404);
                json.setMsg("取消赞同帖子失败！！！");
            }
        }
        json.setData(null);
        return json;
    }

    /**
     * 获取 关注 && 被关注 列表
     *
     * @param fromUid 用户 id
     * @param start   起始位置
     * @param end     结束位置
     * @return 关注 && 被关注列表
     */
    @GetMapping("/user")
    public JsonResult user(@RequestParam("fromUid") String fromUid,
                           @RequestParam("start") int start,
                           @RequestParam("end") int end) {
        JsonResult json = new JsonResult();
        List<Like> concernList = likeService.findAllConcern(fromUid, start, end);
        List<Like> concernedList = likeService.findAllConcerned(fromUid, start, end);
        for (Like like : concernList) {
            like.getFromUid().setPassword(null);
            like.getToUid().setPassword(null);
        }
        for (Like like : concernedList) {
            like.getFromUid().setPassword(null);
            like.getToUid().setPassword(null);
        }
        if (concernList.isEmpty() || concernedList.isEmpty()) {
            json.setCode(404);
            json.setMsg("未获取到关注和被关注用户列表！！！");
        } else {
            json.setCode(200);
            json.setMsg("查询关注和被关注用户列表成功！！！");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("concern", concernList);
        map.put("concerned", concernedList);
        json.setData(map);
        return json;
    }

    /**
     * 获取关注列表
     *
     * @param fromUid 用户 id
     * @param start   起始位置
     * @param end     结束位置
     * @return 关注列表
     */
    @GetMapping("/article")
    public JsonResult article(@RequestParam("fromUid") String fromUid,
                              @RequestParam("start") int start,
                              @RequestParam("end") int end) {
        JsonResult json = new JsonResult();
        List<Like> list = likeService.findConcernArticle(fromUid, start, end);
        for (Like like : list) {
            like.getFromUid().setPassword(null);
        }
        if (list.isEmpty()) {
            json.setCode(404);
            json.setMsg("未获取到喜欢的文章列表！！！");
        } else {
            json.setCode(200);
            json.setMsg("查询喜欢的文章列表成功！！！");
        }
        json.setData(list);
        return json;
    }

}
